package cn.broadsense.thrid.api;

import cn.broadsense.common.exception.ServiceException;
import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.*;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

/**
 * 人像智能相关
 *
 * @author pengxin
 * @date 2022-10-12 15:26
 */
@Slf4j
@Component
public class PeopleAiRepository extends BaseRepository {


    /**
     * 新增人像库
     *
     * @param param
     * @return
     */
    public String addFaceRepository(FaceRepositoryAddParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getString("id");
    }

    /**
     * 人像库列表
     *
     * @param param
     * @return
     */
    public FaceRepositoryListResult faceRepositoryList(FaceRepositoryQueryParam param) {

        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(param);
        stringObjectMap.put("resultClass", null);
        stringObjectMap.put("ignoreList", null);
        stringObjectMap.put("url", null);
        String query = MapUtil.joinIgnoreNull(stringObjectMap, "&", "=");
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY + "?" + query);
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 人像库详情
     *
     * @param repositoryId
     * @return
     */
    public FaceRepositoryResult faceRepositoryDetails(String repositoryId) {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return FaceRepositoryResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY_EDIT, repositoryId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 库人员新增
     *
     * @param param
     * @return
     */
    public String addFaceRepoMember(FaceRepoMemberAddParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY_MEMBER);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("id")) {
            return result.getString("id");
        }
        log.error("添加库成员失败 接口返回结果：{}", result.toJSONString());
        throw new ServiceException("添加库成员失败");
    }

    /**
     * 更新人脸库人员
     *
     * @param memberId
     * @param param
     */
    public void updateFaceRepoMember(String memberId, FaceRepoMemberAddParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY_MEMBER_EDIT, memberId));
        DaHuaHttpUtils.putRest(getCToken(), param);
    }

    /**
     * 删除人脸库人员
     *
     * @param memberId
     */
    public void delFaceRepoMember(String memberId) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return String.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY_MEMBER_EDIT, memberId));
        DaHuaHttpUtils.deleteRest(getCToken(), param);
    }

    /**
     * 查询库人员详情
     *
     * @param memberId
     * @return
     */
    public FaceRepoMemberDetailsResult faceRepoMemberDetails(String memberId) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return FaceRepoMemberDetailsResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_REPOSITORY_MEMBER_EDIT, memberId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 布控设置
     *
     * @param param
     */
    public void faceSurveillance(FaceSurveillanceAddParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_SURVEILLANCE);
        DaHuaHttpUtils.putRest(getCToken(), param);
    }

    /**
     * 布控设置批量
     *
     * @param param
     */
    public void faceBatchSurveillance(FaceBatchSurveillanceParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_BATCH_SURVEILLANCE);
        DaHuaHttpUtils.putRest(getCToken(), param);
    }

    /**
     * 布控删除
     *
     * @param repositoryId 人像库id
     * @param channelCode  点位编码
     */
    public void delFaceSurveillance(String repositoryId, String channelCode) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return JSONObject.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.DEL_FACE_SURVEILLANCE, repositoryId, channelCode));
        DaHuaHttpUtils.deleteRest(getCToken(), param);
    }

    /**
     * 人像布控列表
     *
     * @param param
     * @return
     */
    public FaceSurveillanceListResult faceSurveillanceList(FaceSurveillanceListParam param) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(param, new TreeMap<>(), true, true);
        stringObjectMap.put("resultClass", null);
        stringObjectMap.put("ignoreList", null);
        stringObjectMap.put("url", null);
        String query = MapUtil.joinIgnoreNull(stringObjectMap, "&", "=");
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_SURVEILLANCE + "?" + query);
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 人像告警记录列表
     *
     * @param param
     * @return
     */
    public FaceAlarmListResult faceAlarmList(FaceAlarmListParam param) {
        int page = param.getPage();
        int pageSize = param.getPageSize();
        int start = (page - 1) * pageSize + 1;
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_ALARM_2);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 人像告警记录详情
     *
     * @param recordId 记录id
     * @return
     */
    public FaceAlarmDetailsResult faceAlarmDetails(String recordId) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return FaceAlarmDetailsResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_ALARM_DETAILS, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 人像抓拍记录查询
     *
     * @param param
     * @return
     */
    public FaceCaptureResult faceCapture(FaceCaptureParam param) {
        try {
            param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_CAPTURE);
            return DaHuaHttpUtils.postRest(getCToken(), param);
        } catch (Exception e) {
            log.error("获取人像数据失败：{}", e.getMessage());
            return new FaceCaptureResult();
        }

    }

    /**
     * 人像抓拍记录详情
     *
     * @param recordId 记录id
     * @return
     */
    public FaceCaptureDetailsResult faceCaptureDetails(String recordId, String capTime) {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return FaceCaptureDetailsResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.PeopleAi.FACE_CAPTURE_DETAILS + capTime, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 检索和查询人像库。本接口支持以图搜图和条件查询两种功能，每次只能执行其中一种查询。
     *
     * @param param
     * @return
     */
    public FaceRepoSearchResult faceRepoSearch(FaceRepoSearchParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_REPO_SEARCH);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 人像告警统计
     *
     * @param param
     * @return
     */
    public Integer faceAlarmStatistics(FaceStatisticsParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_STATISTICS_ALARM);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("totalCount")) {
            return result.getInteger("totalCount");
        }
        log.error("faceAlarmStatistics 查询失败：{}", result.toJSONString());
        return null;
    }

    /**
     * 人脸抓拍统计
     *
     * @param param
     * @return
     */
    public Integer faceCaptureStatistics(FaceStatisticsParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_STATISTICS_CAPTURE);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("totalCount")) {
            return result.getInteger("totalCount");
        }
        log.error("faceCaptureStatistics 查询失败：{}", result.toJSONString());
        return null;
    }

    /**
     * 人像告警总数查询
     *
     * @param param
     * @return
     */
    public Integer faceAlarmCount(FaceCountParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_ALARM_COUNT);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("totalCount")) {
            return result.getInteger("totalCount");
        }
        log.error("faceAlarmCount 查询失败：{}", result.toJSONString());
        return null;
    }

    /**
     * 人像抓拍总数查询
     *
     * @param param
     * @return
     */
    public Integer faceCaptureCount(FaceCountParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_CAPTURE_COUNT);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("totalCount")) {
            return result.getInteger("totalCount");
        }
        log.error("faceCaptureCount 查询失败：{}", result.toJSONString());
        return null;
    }

    /**
     * 根据人像库类型来统计库内的人员总数。 目前接口只支持统计全部,types为空。
     *
     * @param param
     * @return
     */
    public FaceStatistRepositoryResult faceStatistRepository(FaceStatistRepositoryParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_STATISTICS_REPOSITORY);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 人脸抓拍统计高级接口
     *
     * @param param
     * @return
     */
    public FaceCaptureCountResult faceCaptureCount(FaceCaptureCountParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_FEATURE_CAPTURE_COUNT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 抓拍记录统计
     *
     * @param param
     * @return
     */
    public FaceCaptureRecResult faceCaptureRec(FaceCaptureRecParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_RECORD_CAPTURE);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 人像告警按日统计
     *
     * @param param
     * @return
     */
    public FaceAlarmDayResult faceAlarmDay(FaceAlarmDayParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_ALARM_STATISTICS_DAY);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 1v1 图片比对
     *
     * @param param
     * @return
     */
    public BigDecimal faceVerify(FaceVerifyParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_VERIFY);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("similarity")) {
            return result.getBigDecimal("similarity");
        }
        return BigDecimal.ZERO;
    }
}
